<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'

const router = useRouter()
const username = ref('')
const password = ref('')
const rememberMe = ref(false)
const errorMessage = ref('')
const isLoading = ref(false)
const showPassword = ref(false)

// 组件挂载时检查是否有保存的用户名
onMounted(() => {
  const savedUsername = localStorage.getItem('username')
  if (savedUsername) {
    username.value = savedUsername
    rememberMe.value = true
  }
})

// 表单验证
const validateForm = () => {
  if (!username.value.trim()) {
    errorMessage.value = '请输入用户名'
    return false
  }
  if (!password.value) {
    errorMessage.value = '请输入密码'
    return false
  }
  return true
}

// 登录处理函数
const handleLogin = async () => {
  if (!validateForm()) {
    return
  }
  
  // 模拟登录过程
  errorMessage.value = ''
  isLoading.value = true
  
  try {
    // 模拟API请求延迟
    await new Promise(resolve => setTimeout(resolve, 800))
    
    // 这里只是简单的模拟，实际项目中应该调用后端API
    if (username.value && password.value) {
      // 存储登录状态
      localStorage.setItem('isLoggedIn', 'true')
      if (rememberMe.value) {
        localStorage.setItem('username', username.value)
      } else {
        localStorage.removeItem('username')
      }
      
      // 登录成功后跳转到首页
      router.push('/')
    } else {
      errorMessage.value = '用户名或密码错误'
    }
  } catch (error) {
    errorMessage.value = '登录失败，请稍后重试'
  } finally {
    isLoading.value = false
  }
}

// 切换密码可见性
const togglePasswordVisibility = () => {
  showPassword.value = !showPassword.value
}
</script>

<template>
  <div class="login-page">
    <!-- 背景装饰 -->
    <div class="login-background">
      <div class="bg-gradient"></div>
      <div class="bg-pattern"></div>
    </div>
    
    <!-- 登录卡片 -->
    <div class="login-container">
      <div class="login-card">
        <!-- 登录标题和Logo -->
        <div class="login-header">
          <div class="login-logo">
            <svg width="48" height="48" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
              <path d="M21 12C21 16.9706 16.9706 21 12 21C7.02944 21 3 16.9706 3 12C3 7.02944 7.02944 3 12 3C16.9706 3 21 7.02944 21 12Z" stroke="#3B82F6" stroke-width="2"/>
              <path d="M12 8V12L15 15" stroke="#3B82F6" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
            </svg>
          </div>
          <h1 class="login-title">欢迎回来</h1>
          <p class="login-subtitle">请登录您的账户继续</p>
        </div>
        
        <!-- 登录表单 -->
        <form class="login-form" @submit.prevent="handleLogin">
          <!-- 错误消息显示 -->
          <div v-if="errorMessage" class="error-message">
            <svg class="error-icon" width="16" height="16" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
              <path d="M12 9V11" stroke="currentColor" stroke-width="2" stroke-linecap="round"/>
              <path d="M12 15H12.01" stroke="currentColor" stroke-width="2" stroke-linecap="round"/>
              <circle cx="12" cy="12" r="9" stroke="currentColor" stroke-width="2"/>
              <path d="M12 22C17.5228 22 22 17.5228 22 12C22 6.47715 17.5228 2 12 2C6.47715 2 2 6.47715 2 12C2 17.5228 6.47715 22 12 22Z" stroke="currentColor" stroke-width="2"/>
            </svg>
            {{ errorMessage }}
          </div>
          
          <!-- 用户名输入 -->
          <div class="form-group">
            <label for="username" class="form-label">
              <span class="label-text">用户名</span>
              <span class="required">*</span>
            </label>
            <div class="input-wrapper">
              <div class="input-icon">
                <svg width="18" height="18" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                  <path d="M20 21V19C20 17.9391 19.5786 16.9217 18.8284 16.1716C18.0783 15.4214 17.0609 15 16 15H8C6.93913 15 5.92172 15.4214 5.17157 16.1716C4.42143 16.9217 4 17.9391 4 19V21" stroke="#64748B" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                  <path d="M12 11C14.2091 11 16 9.20914 16 7C16 4.79086 14.2091 3 12 3C9.79086 3 8 4.79086 8 7C8 9.20914 9.79086 11 12 11Z" stroke="#64748B" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                </svg>
              </div>
              <input
                type="text"
                id="username"
                v-model="username"
                placeholder="请输入用户名"
                @input="errorMessage = ''"
                class="form-input"
                :class="{ 'error': errorMessage && !username.trim() }"
                autocomplete="username"
              />
            </div>
          </div>
          
          <!-- 密码输入 -->
          <div class="form-group">
            <label for="password" class="form-label">
              <span class="label-text">密码</span>
              <span class="required">*</span>
            </label>
            <div class="input-wrapper">
              <div class="input-icon">
                <svg width="18" height="18" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                  <path d="M12 15V17M6.5 10C8.70914 8.47377 11.2909 8.47377 13.5 10M21 12C21 16.9706 16.9706 21 12 21C7.02944 21 3 16.9706 3 12C3 7.02944 7.02944 3 12 3C16.9706 3 21 7.02944 21 12Z" stroke="#64748B" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                  <path d="M12 12C13.6569 12 15 10.6569 15 9C15 7.34315 13.6569 6 12 6C10.3431 6 9 7.34315 9 9C9 10.6569 10.3431 12 12 12Z" stroke="#64748B" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                </svg>
              </div>
              <input
                :type="showPassword ? 'text' : 'password'"
                id="password"
                v-model="password"
                placeholder="请输入密码"
                @input="errorMessage = ''"
                class="form-input"
                :class="{ 'error': errorMessage && !password }"
                autocomplete="current-password"
              />
              <button
                type="button"
                class="toggle-password"
                @click="togglePasswordVisibility"
                aria-label="切换密码可见性"
              >
                <svg v-if="!showPassword" width="18" height="18" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                  <path d="M12 15C13.6569 15 15 13.6569 15 12C15 10.3431 13.6569 9 12 9C10.3431 9 9 10.3431 9 12C9 13.6569 10.3431 15 12 15Z" stroke="#64748B" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                  <path d="M2 12C2 17.5228 6.47715 22 12 22C17.5228 22 22 17.5228 22 12C22 6.47715 17.5228 2 12 2C6.47715 2 2 6.47715 2 12Z" stroke="#64748B" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                </svg>
                <svg v-else width="18" height="18" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                  <path d="M3.27 3.27L20.73 20.73M2 12H2.01M12 2.01V2M22 22L20.73 20.73M22 12H21.99M12 22V21.99M2 22L3.27 20.73" stroke="#64748B" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                </svg>
              </button>
            </div>
          </div>
          
          <!-- 选项区域 -->
          <div class="form-options">
            <label class="remember-me">
              <input type="checkbox" v-model="rememberMe" class="checkbox" />
              <span class="checkmark"></span>
              记住我
            </label>
            <a href="#" class="forgot-password">忘记密码？</a>
          </div>
          
          <!-- 登录按钮 -->
          <button
            type="submit"
            class="login-button"
            :disabled="isLoading"
          >
            <span v-if="isLoading" class="loading-spinner"></span>
            <span>登录</span>
          </button>
        </form>
        
        <!-- 注册链接 -->
        <div class="register-section">
          <p class="register-text">
            还没有账号？
            <a href="#" class="register-link">立即注册</a>
          </p>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
/* 基础布局 */
.login-page {
  min-height: 100vh;
  position: relative;
  overflow: hidden;
  display: flex;
  justify-content: center;
  align-items: center;
}

/* 背景设计 */
.login-background {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 0;
}

.bg-gradient {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  opacity: 0.9;
}

.bg-pattern {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-image: 
    radial-gradient(circle at 25px 25px, rgba(255, 255, 255, 0.15) 2%, transparent 0%),
    radial-gradient(circle at 75px 75px, rgba(255, 255, 255, 0.15) 2%, transparent 0%);
  background-size: 100px 100px;
}

/* 登录容器 */
.login-container {
  position: relative;
  z-index: 1;
  width: 100%;
  max-width: 420px;
  padding: 20px;
}

/* 登录卡片 */
.login-card {
  background: white;
  border-radius: 20px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.15);
  padding: 48px;
  transition: transform 0.3s ease, box-shadow 0.3s ease;
}

.login-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 25px 70px rgba(0, 0, 0, 0.2);
}

/* 登录头部 */
.login-header {
  text-align: center;
  margin-bottom: 40px;
}

.login-logo {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 80px;
  height: 80px;
  margin: 0 auto 24px;
  background: #EFF6FF;
  border-radius: 20px;
  box-shadow: 0 10px 30px rgba(59, 130, 246, 0.2);
}

.login-title {
  font-size: 28px;
  font-weight: 700;
  color: #1E293B;
  margin: 0 0 8px;
  background: linear-gradient(90deg, #1E293B, #475569);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.login-subtitle {
  font-size: 16px;
  color: #64748B;
  margin: 0;
  font-weight: 400;
}

/* 登录表单 */
.login-form {
  display: flex;
  flex-direction: column;
  gap: 24px;
}

/* 错误消息 */
.error-message {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 16px;
  background: #FEF2F2;
  border: 1px solid #FECACA;
  border-radius: 8px;
  color: #EF4444;
  font-size: 14px;
  animation: shake 0.3s ease;
}

.error-icon {
  flex-shrink: 0;
}

/* 表单组 */
.form-group {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.form-label {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 14px;
  font-weight: 600;
  color: #334155;
}

.label-text {
  flex: 1;
}

.required {
  color: #EF4444;
  font-weight: 700;
}

/* 输入包装器 */
.input-wrapper {
  position: relative;
  width: 100%;
}

.input-icon {
  position: absolute;
  left: 16px;
  top: 50%;
  transform: translateY(-50%);
  color: #64748B;
  z-index: 1;
}

/* 输入框样式 */
.form-input {
  width: 100%;
  padding: 14px 16px 14px 48px;
  border: 2px solid #E2E8F0;
  border-radius: 12px;
  font-size: 16px;
  font-weight: 400;
  color: #1E293B;
  background: white;
  transition: all 0.3s ease;
  box-sizing: border-box;
}

.form-input:focus {
  outline: none;
  border-color: #3B82F6;
  box-shadow: 0 0 0 4px rgba(59, 130, 246, 0.1);
}

.form-input::placeholder {
  color: #94A3B8;
}

.form-input.error {
  border-color: #EF4444;
}

.form-input.error:focus {
  box-shadow: 0 0 0 4px rgba(239, 68, 68, 0.1);
}

/* 密码切换按钮 */
.toggle-password {
  position: absolute;
  right: 16px;
  top: 50%;
  transform: translateY(-50%);
  background: none;
  border: none;
  cursor: pointer;
  padding: 4px;
  border-radius: 4px;
  transition: background-color 0.2s ease;
  z-index: 1;
}

.toggle-password:hover {
  background-color: #F1F5F9;
}

/* 表单选项 */
.form-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 4px;
}

/* 记住我复选框 */
.remember-me {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  position: relative;
  font-size: 14px;
  font-weight: 500;
  color: #475569;
}

.checkbox {
  opacity: 0;
  width: 0;
  height: 0;
}

.checkmark {
  position: relative;
  width: 20px;
  height: 20px;
  background-color: #F1F5F9;
  border: 2px solid #E2E8F0;
  border-radius: 6px;
  transition: all 0.3s ease;
}

.remember-me:hover .checkmark {
  border-color: #CBD5E1;
}

.checkbox:checked ~ .checkmark {
  background-color: #3B82F6;
  border-color: #3B82F6;
}

.checkmark:after {
  content: "";
  position: absolute;
  display: none;
  left: 6px;
  top: 2px;
  width: 6px;
  height: 12px;
  border: solid white;
  border-width: 0 2px 2px 0;
  transform: rotate(45deg);
}

.checkbox:checked ~ .checkmark:after {
  display: block;
}

/* 忘记密码链接 */
.forgot-password {
  font-size: 14px;
  font-weight: 500;
  color: #3B82F6;
  text-decoration: none;
  transition: all 0.3s ease;
}

.forgot-password:hover {
  color: #2563EB;
  text-decoration: underline;
}

/* 登录按钮 */
.login-button {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  width: 100%;
  padding: 16px;
  background: linear-gradient(90deg, #3B82F6, #2563EB);
  border: none;
  border-radius: 12px;
  font-size: 16px;
  font-weight: 600;
  color: white;
  cursor: pointer;
  transition: all 0.3s ease;
  position: relative;
  overflow: hidden;
}

.login-button:hover:not(:disabled) {
  background: linear-gradient(90deg, #2563EB, #1D4ED8);
  transform: translateY(-1px);
  box-shadow: 0 10px 25px -5px rgba(59, 130, 246, 0.4);
}

.login-button:active:not(:disabled) {
  transform: translateY(0);
}

.login-button:disabled {
  opacity: 0.7;
  cursor: not-allowed;
}

/* 加载动画 */
.loading-spinner {
  width: 16px;
  height: 16px;
  border: 2px solid rgba(255, 255, 255, 0.3);
  border-top: 2px solid white;
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

/* 注册区域 */
.register-section {
  margin-top: 32px;
  text-align: center;
}

.register-text {
  font-size: 14px;
  color: #64748B;
  margin: 0;
}

.register-link {
  font-weight: 600;
  color: #3B82F6;
  text-decoration: none;
  transition: all 0.3s ease;
}

.register-link:hover {
  color: #2563EB;
  text-decoration: underline;
}

/* 动画效果 */
@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20%, 60% { transform: translateX(-5px); }
  40%, 80% { transform: translateX(5px); }
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

/* 响应式设计 */
@media (max-width: 768px) {
  .login-container {
    padding: 20px;
  }
  
  .login-card {
    padding: 40px 28px;
    border-radius: 16px;
  }
  
  .login-title {
    font-size: 24px;
  }
  
  .login-logo {
    width: 70px;
    height: 70px;
  }
}

@media (max-width: 480px) {
  .login-card {
    padding: 32px 20px;
    border-radius: 12px;
  }
  
  .login-form {
    gap: 20px;
  }
  
  .form-input {
    padding: 12px 14px 12px 42px;
    font-size: 15px;
  }
  
  .login-button {
    padding: 14px;
    font-size: 15px;
  }
}
</style>